package ws.spider.achievementPoints;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import ws.spider.util.ArmoryQueryEngine;
import ws.spider.util.ArmoryQueryEngineException;


public class Spider 
{
	public final static String GUILDS_FILE = "data/guilds.properties";
	public final static String LOG_FILE = "data/wowstats.log";
	public final static String ACHPOINTS_FILE = "data/achPoints.txt";
	public final static String PLAYERACH_FILE = "data/playerAchPoints.txt";

	private ArrayList<String> achPointsList;
	private ArrayList<String> playerAchievs;
	private ArrayList<String> queue;
	
	public Spider( ) throws Exception
	{
		achPointsList = new ArrayList<String>( );
		playerAchievs = new ArrayList<String>( );
		queue = new ArrayList<String>( );
		
		try {
			loadGuildList( GUILDS_FILE );
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		this.startSpider();
		this.saveInfo(ACHPOINTS_FILE, achPointsList);
		this.saveInfo(PLAYERACH_FILE, playerAchievs);
	}
	
	public void startSpider( )
	{
		ArmoryQueryEngine engine = new ArmoryQueryEngine( );
		for( String guild : queue )
		{
			System.out.println( "Fetching data from " + guild );
			try 
			{
				ArrayList<String> members = engine.getGuildInfo( guild );
				for( String member : members )
				{
					System.out.println( "	Fetching data from " + member );
					try 
					{
						String[] attrs = { "points", "faction", "class" };
						Hashtable<String,String> values = engine.getPlayerInfo( member, attrs );
						achPointsList.add( guild + ";" + member + ";" + values.get(attrs[0]) + ";" + values.get(attrs[1]).charAt(0) + ";" + values.get(attrs[2]) );
					} 
					catch ( ArmoryQueryEngineException e ) 
					{
						System.out.println( e.getMessage());
					}
					
					try 
					{
						ArrayList<String> achievs = engine.getPlayerAchievs( member, 168 );
						for( String achiev : achievs )
							playerAchievs.add( member + ";" + achiev );
					} 
					catch ( ArmoryQueryEngineException e ) 
					{
						System.out.println( e.getMessage());
					}
				}
			} 
			catch ( ArmoryQueryEngineException e ) 
			{
				System.out.println( e.getMessage());
			}
		}
	}
	
	private void loadGuildList( String arch ) throws Exception
    {
        try
        {
        	Properties data = new Properties( );
            FileInputStream in = new FileInputStream( new File( arch ) );
            
            data.load( in );
            in.close( );
            
            int numGuilds = Integer.parseInt( data.getProperty( "numGuilds" ) );
            
            System.out.println( "Found " + numGuilds + " guilds" );
            
            for( int i = 0; i < numGuilds; i++ )
                queue.add( data.getProperty( "guild" + i ) );
        }
        catch( Exception e )
        {
            throw new Exception( "Invalid format" );
        }
    } 
	
	public static void main( String args[] )
	{
		try {
			new Spider();
		} catch ( Exception e ) {
			System.out.println( "Failed to start the spider" );
		}
	}
	
	private void saveInfo( String fileName, ArrayList<String> data )
	{
		File archivoFactura = new File( fileName );
        try 
        {
			PrintWriter out = new PrintWriter( archivoFactura );
			
			for( String s : data )
				out.println( s );
			
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

}
